package com.example.hao.ximalaya.fragment;


import android.os.Bundle;
import android.os.Handler;

import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.hao.ximalaya.R;
import com.example.hao.ximalaya.adapter.HotAdapter;
import com.example.hao.ximalaya.pojo.HotData;
import com.example.hao.ximalaya.utils.HttpUtils;
import com.google.gson.Gson;

import java.io.IOException;



/**
 * A simple {@link Fragment} subclass.
 */
public class RecommendFragment extends Fragment {
    private static String TAG = "Hao";
    private RecyclerView rv_recommend;
    private HotAdapter hotAdapter;

    public RecommendFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View layout = inflater.inflate(R.layout.fragment_recommend, container, false);

        rv_recommend = layout.findViewById(R.id.rv_recommend);
        initView();
        loadData();
        return layout;
    }

    private void initView() {
        rv_recommend.setLayoutManager(new LinearLayoutManager(getActivity()));
        hotAdapter = new HotAdapter(getActivity());
        rv_recommend.setAdapter(hotAdapter);
    }


    private Handler h = new Handler(new Handler.Callback() {
        /**
         * 接受处理消息
         * @param msg
         * @return
         */
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 1://消息what==1
                    //获取消息绑定的 obj 对象
                    HotData hotData = (HotData) msg.obj;
                    //绑定数据到适配器
                    hotAdapter.setHotData(hotData);
                    break;
                case 999:
                    String err = (String) msg.obj;
                    Toast.makeText(getContext(), err, Toast.LENGTH_SHORT).show();
                    break;
            }
            return false;
        }
    });

    private void loadData() {
//        start1();
//        start2();
//        start();
        start3();
    }

    private void start() {

        HttpUtils.method1(HttpUtils.URL_RECOMMEND, new HttpUtils.onAcceptCallBack() {
            @Override
            public void onAccept(String s) {
                HotData hotData = new Gson().fromJson(s, HotData.class);

                Message message = h.obtainMessage(1, hotData);//创建消息,what=1,obj=hotData

                h.sendMessage(message);//发送消息
            }

            @Override
            public void onAccept(Throwable throwable) {
                Toast.makeText(getContext(), "请求失败", Toast.LENGTH_SHORT).show();
            }
        });


    }





    private void start2() {
        HttpUtils.okHttpGetAsync(HttpUtils.URL_RECOMMEND, new HttpUtils.OnOkCallBack() {
            @Override
            public void onFailure(@NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull String s) {

                        HotData hotData = new Gson().fromJson(s, HotData.class);

                        Message message = h.obtainMessage(1, hotData);//创建消息,what=1,obj=hotData

                        h.sendMessage(message);//发送消息
                    }

        });
    }

    private void start1() {
        new Thread(new Runnable() {
            @Override
            public void run() {
//
//                Looper.prepare();
//                String s = HttpUtils.get(HttpUtils.URL_RECOMMEND);
                String s = HttpUtils.okHttpGetSync(HttpUtils.URL_RECOMMEND);

                if (s == null) {
                    Message message = h.obtainMessage(999, "网络请求异常");
                    h.sendMessage(message);
                }
                Log.e(TAG, "run: " + s);
                //解析json
                HotData hotData = new Gson().fromJson(s, HotData.class);

                Message message = h.obtainMessage(1, hotData);//创建消息,what=1,obj=hotData

                h.sendMessage(message);//发送消息
            }
        }).start();
    }



    private void start3(){
        HttpUtils.method2(HttpUtils.URL_RECOMMEND, new HttpUtils.OnOkCallBack() {
            @Override
            public void onFailure(@NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull String jsonStr) {
                HotData hotData = new Gson().fromJson(jsonStr, HotData.class);

                Message message = h.obtainMessage(1, hotData);//创建消息,what=1,obj=hotData

                h.sendMessage(message);//发送消息
            }
        });
    }

}
